package com.example.permissionsystem1_1.businessFunction.service.interfaces.impl;

import com.example.permissionsystem1_1.businessFunction.dao.entity.po.*;
import com.example.permissionsystem1_1.businessFunction.dao.repository.DepartmentInfoDao;
import com.example.permissionsystem1_1.businessFunction.dao.repository.DepartmentUserDao;
import com.example.permissionsystem1_1.businessFunction.dao.repository.UserInfoDao;
import com.example.permissionsystem1_1.businessFunction.service.converter.DepartmentUserServiceConverter;
import com.example.permissionsystem1_1.businessFunction.service.converter.UserRoleServiceConverter;
import com.example.permissionsystem1_1.businessFunction.service.entity.reqModel.departmentUserReqModel.DepartmentUserSelectAllDepartmentsByUserIdReqModel;
import com.example.permissionsystem1_1.businessFunction.service.entity.reqModel.departmentUserReqModel.DepartmentUserSetDepartmentUserRelationReqModel;
import com.example.permissionsystem1_1.businessFunction.service.entity.rspModel.departmentUserRspModel.DepartmentUserSelectAllDepartmentsByUserIdRspModel;
import com.example.permissionsystem1_1.businessFunction.service.entity.rspModel.departmentUserRspModel.DepartmentUserSetDepartmentUserRelationRspModel;
import com.example.permissionsystem1_1.businessFunction.service.interfaces.DepartmentUserService;
import com.example.permissionsystem1_1.common.businessFunction.BaseServiceImpl;
import com.example.permissionsystem1_1.system.annotation.aspectAnnotation.SuperManageOperationAspectAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DepartmentUserServiceImpl extends BaseServiceImpl implements DepartmentUserService {
    @Autowired
    private DepartmentUserDao departmentUserDao;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private DepartmentInfoDao departmentInfoDao;

    @Override
    @SuperManageOperationAspectAnnotation
    public DepartmentUserSelectAllDepartmentsByUserIdRspModel selectAllDepartmentsByUserId(DepartmentUserSelectAllDepartmentsByUserIdReqModel reqModel) {
        DepartmentUserPo po=DepartmentUserServiceConverter.convertReqModelToPo(reqModel);
        List<DepartmentUserPo> pos=null;
        if(po!=null&&po.getUserId()!=null){
            pos=departmentUserDao.selectAllDepartmentsByUserId(po.getUserId());
        }
        return DepartmentUserServiceConverter.covertToSelectAllDepartmentsByUserIdModel(pos);
    }

    @Override
    @Transactional
    @SuperManageOperationAspectAnnotation
    public DepartmentUserSetDepartmentUserRelationRspModel setDepartmentUserRelation(DepartmentUserSetDepartmentUserRelationReqModel reqModel) {
        DepartmentUserPo po=DepartmentUserServiceConverter.convertReqModelToPo(reqModel);
        int i=-1;
        if(po==null){
            return null;
        }
        DepartmentUserPo departmentUserPo=new DepartmentUserPo();
        departmentUserPo.setUserId(po.getUserId());
        int i1=departmentUserDao.deletes(departmentUserPo);//无论是否有这个用户,都进行一下数据清理,以便保证
        Long userId=po.getUserId();
        UserInfoPo userInfoPo=userInfoDao.selectById(userId);
        DepartmentInfoPo departmentInfoPo=departmentInfoDao.selectById(po.getDepartmentId());
        if(userInfoPo==null||departmentInfoPo==null){
            //仅仅做了删除原始关联,而没有对指定用户添加任何部门
            return DepartmentUserServiceConverter.covertToSetDepartmentUserRelationModel(i1);
        }
        List<DepartmentUserPo> pos=new ArrayList<>();
        pos.add(po);//此po中的userId和departmentId都是有对应的真实用户和部门
        int i2=departmentUserDao.insertBatch(pos);
        i=i1+i2;
        return DepartmentUserServiceConverter.covertToSetDepartmentUserRelationModel(i);
    }
}
